package com.example.waibao.config;

import com.example.waibao.entity.rank.RankDO;
import com.example.waibao.entity.rank.rank_score;
import com.example.waibao.entity.rank.speciality_rank_score;
import com.example.waibao.entity.student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisComponent {
    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 添加一个元素，zset与set最大的区别就是每个元素都有一个score，因此有个排序的辅助功能;(zadd)
     *
     * @param key
     * @param value
     * @param score
     */
    public void add(String key, String value, double score) {

        redisTemplate.opsForZSet().add(key, value, score);

    }

    /**
     * 添加一个列表元素，
     *
     * @param key
     * @param rankAll
     */
    public void  addAll(String key, List<rank_score> rankAll, int type) {

        for (rank_score rank_score : rankAll ) {
          //  float score = -rank_score.getSum_score();

            //分数相同的看完成时间，用的时间越少，排名越靠前
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date last_income_time = null;
            Date create_time = null;
            try {
                last_income_time = sdf.parse(rank_score.getLast_incom_time());
                create_time = sdf.parse(rank_score.getCreate_time());
            } catch (ParseException e) {
                e.printStackTrace();
            }

            long score_time = last_income_time.getTime()/1000 - create_time.getTime()/1000;
            Double d = 1.0;
            String aaa = "0" + '.'+score_time;
            Float s2 = Float.valueOf(aaa);
            Double score_time2 = d-s2;


//            String sum_score = String.valueOf(rank_score.getSum_score().intValue()) + '.'+score_time2.intValue();
//            Float all_score = -Float.valueOf(sum_score);

            Double all_score2 = rank_score.getSum_score()+score_time2;

         //   System.out.println(score);
          //  redisTemplate.opsForZSet().add(key, rank_score.getName(), score);
            if(type == 1){
                redisTemplate.opsForZSet().add(key, rank_score.getName(), -all_score2);
            }else {
                redisTemplate.opsForZSet().add(key, rank_score.getName(), all_score2);
            }

        }

        redisTemplate.expire(key,  60, TimeUnit.SECONDS); //过期时间设置为分钟，也就是一分钟一更新排行榜
//        redisTemplate.opsForZSet().add(key, value, score);
    }

    public void addAll2(String key, List<speciality_rank_score> rankAll) {

        for (speciality_rank_score rank_score : rankAll ) {
//            float score = -rank_score.getSum_score();
//            System.out.println(score);
//            redisTemplate.opsForZSet().add(key, rank_score.getType(), score);


            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date last_income_time = null;
            Date create_time = null;
            try {
                last_income_time = sdf.parse(rank_score.getLast_incom_time());
                create_time = sdf.parse(rank_score.getCreate_time());
            } catch (ParseException e) {
                e.printStackTrace();
            }

            long score_time = last_income_time.getTime()/1000 - create_time.getTime()/1000;
            Double d = 1.0;
            String aaa = "0" + '.'+score_time;
            Float s2 = Float.valueOf(aaa);
            Double score_time2 = d-s2;


//            String sum_score = String.valueOf(rank_score.getSum_score().intValue()) + '.'+score_time2.intValue();
//            Float all_score = -Float.valueOf(sum_score);

            System.out.println(rank_score.getType() + "----"+ rank_score.getSum_score());
            Double all_score2 = rank_score.getSum_score()+score_time2;

            //   System.out.println(score);
            //  redisTemplate.opsForZSet().add(key, rank_score.getName(), score);
            redisTemplate.opsForZSet().add(key, rank_score.getType(), -all_score2);

        }

        redisTemplate.expire(key,  60, TimeUnit.SECONDS); //过期时间设置为一分钟，也就是一分钟一更新排行榜
//        redisTemplate.opsForZSet().add(key, value, score);
    }
    /**
     * 删除元素 zrem
     *
     * @param key
     * @param value
     */
    public void remove(String key, String value) {

        redisTemplate.opsForZSet().remove(key, value);
    }

    /**
     * score 的增加或减少 zincrby
     *
     * @param key
     * @param value
     * @param score
     * @return
     */
    public Double incrScore(String key, String value, double score) {

        return redisTemplate.opsForZSet().incrementScore(key, value, score);
    }

    /**
     * 查询value对应的score -- zscore
     *
     * @param key
     * @param value
     * @return
     */
    public Double score(String key, String value) {

        return redisTemplate.opsForZSet().score(key, value);
    }

    /**
     * 判断value 在 zset 中的排名 -- zrank
     *
     * @param key
     * @param value
     * @return
     */
    public Long rank(String key, String value) {

        return redisTemplate.opsForZSet().rank(key, value);
    }

    /**
     * 查询集合中指定顺序的值，0 -1 表示获取全部的集合内容 zrange
     *
     * 返回有序的集合，score小的在前面
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<String> range(String key, long start, long end) {

        return redisTemplate.opsForZSet().range(key, start, end);
    }

    /**
     *
     * 查询集合中指定顺序的值和score，0, -1 表示获取全部的集合内容
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<ZSetOperations.TypedTuple<String>> rangeWithScore(String key, long start, long end) {

        return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
    }

    /**
     * 查询集合中指定顺序的值  zrevrange
     *
     * 返回有序的集合中，score大的在前面
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<String> revRange(String key, long start, long end) {

        return redisTemplate.opsForZSet().reverseRange(key, start, end);
    }

    /**
     * 根据score的值，来获取满足条件的集合  zrangebyscore
     *
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Set<String> sortRange(String key, long min, long max) {
        return redisTemplate.opsForZSet().rangeByScore(key, min, max);
    }

    /**
     * 返回集合的长度
     *
     * @param key
     * @return
     */
    public Long size(String key) {
        return redisTemplate.opsForZSet().zCard(key);
    }

}
